<template>
  <div>
    <div class="crumbs">
      <el-form ref="form" :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="文章标题/作者">
          <el-input v-model="formInline.keywords" placeholder="文章标题/作者"></el-input>
        </el-form-item>
        <el-form-item label="文章分类">
          <el-cascader clearable change-on-select v-model="formInline.cateIds" :options="tree" :props="defaultProps" :show-all-levels="false"></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSearch('form')">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100"></el-table-column>
      <el-table-column prop="title" label="名称" width="400"></el-table-column>
      <el-table-column prop="name" label="所属分类"></el-table-column>
      <el-table-column prop="author" label="作者"></el-table-column>
      <el-table-column prop="create_time" label="创建时间" width="150"></el-table-column>
      <el-table-column prop="status_text" label="状态" width="120"></el-table-column>
      <el-table-column prop="audit_text" label="审核状态" width="150"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="edit(scope.$index, scope.row)">查看/编辑</el-button>
          <el-button size="small" v-if="scope.row.audit_status === 0" @click="pass(scope.$index, scope.row)">审核通过</el-button>
          <el-button size="small" v-if="scope.row.audit_status === 0" @click="failed(scope.$index, scope.row)">审核失败</el-button>
          <el-button size="small" @click="stop(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @current-change ="handleCurrentChange"
        layout="total, prev, pager, next"
        :current-page="textData.current_page"
        :page-size="textData.per_page"
        :total="textData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {mapState} from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data () {
      return {
        defaultProps: {
          value: 'id',
          label: 'name',
          children: '_child'
        },
        formInline: {
          keywords: '',
          cateIds: []
        },
        cateId: 0
      }
    },
    computed: {
      ...mapState({
        tableData: state => state.article.articleList,
        textData: state => state.article.pageData,
        userPdata: state => state.user.info,
        tree: state => state.article.classTree
      })
    },
    created () {
      this.articleList()
      this.articleClassTree()
    },
    methods: {
      // 获取文章分类树
      async articleClassTree () {
        await this.$store.dispatch('article/classTree')
      },
      async articleList () {
        let payload = {
          page: this.textData.current_page,
          listRows: this.textData.per_page,
          classify_id: this.$route.query.cateId,
          keywords: this.$route.query.keywords
        }
        await this.$store.dispatch('article/articleList', payload)
      },
      onSearch (formName) {
        const self = this
        self.$refs[formName].validate((valid) => {
          if (valid) {
            if (Number(self.formInline.cateIds.length) > 0) {
              self.cateId = self.formInline.cateIds[Number(self.formInline.cateIds.length) - 1]
            } else {
              self.cateId = 0
            }
            self.$router.push({path: '/article/articlelist', query: {keywords: self.formInline.keywords, cateId: self.cateId}})
            self.articleList()
          }
        })
      },
      handleCurrentChange (val) {
        this.textData.current_page = val
        this.articleList()
      },
      edit (index, row) {
        this.$router.push({name: 'ArticleEdit', params: {id: row.id}})
      },
      stop (index, row) {
        let payload = {
          id: row.id
        }
        this.$store.dispatch('article/articleDel', payload).then(res => {
          this.articleList()
          notify.success(res.msg)
        })
      },
      pass (index, row) {
        let payload = {
          id: row.id,
          audit_id: this.userPdata.id
        }
        this.$store.dispatch('article/articlePass', payload).then(res => {
          this.articleList()
          notify.success(res.msg)
        })
      },
      failed (index, row) {
        let payload = {
          id: row.id,
          audit_id: this.userPdata.id
        }
        this.$store.dispatch('article/articleFailed', payload).then(res => {
          this.articleList()
          notify.success(res.msg)
        })
      }
    }
  }
</script>
